﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Npgsql;

namespace Emploees
{
    public partial class Time_manage : Form
    {

        string time_hour;
        string time_minute;
        static public NpgsqlConnection conn = Global_variables.connecttodb();
        
        public Time_manage()
        {
            InitializeComponent();
            // Проверяем нажимал ли пользователь "Начало работы" сегодня
            Global_variables.conn.Open();
            NpgsqlCommand command4 = new NpgsqlCommand(Global_variables.sql10, Global_variables.conn);
            command4.Parameters.Add(new NpgsqlParameter("@a", NpgsqlTypes.NpgsqlDbType.Integer));
            command4.Parameters.Add(new NpgsqlParameter("@b", NpgsqlTypes.NpgsqlDbType.Date));
            command4.Parameters[0].Value = Convert.ToInt32(Global_variables.id_user);
            command4.Parameters[1].Value = Convert.ToDateTime(Global_variables.current_date);
            NpgsqlDataReader res = command4.ExecuteReader();
            
            if (res.Read())
            {
                //MessageBox.Show("YES");
                button1.Enabled = false;
                //MessageBox.Show(Convert.ToString(res.GetValue(3)));

                if (!res.IsDBNull(3)) button2.Enabled=false;
            }
            Global_variables.conn.Close();
            timer1.Interval = 30000;
            timer1.Enabled = true;
            this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        }

        private void timer1_Tick(object sender, System.EventArgs e)
        {
            this.CurrenTime();
        }
          
        private void CurrenTime()
        {
            WorkTimeForm.conn.Open();
            NpgsqlCommand command5 = new NpgsqlCommand(Global_variables.sql1, WorkTimeForm.conn);
            NpgsqlDataReader dr1 = command5.ExecuteReader();
            WorkTimeForm.conn.Close();
            if (dr1.Read())
            {
                time_hour = Convert.ToString(dr1.GetValue(0));
            }
            if (time_hour.Length == 1) time_hour = String.Concat("0", time_hour);
            WorkTimeForm.conn.Open();
            NpgsqlCommand command6 = new NpgsqlCommand(Global_variables.sql2, WorkTimeForm.conn);
            NpgsqlDataReader dr2 = command6.ExecuteReader();
            WorkTimeForm.conn.Close();
            if (dr2.Read())
            {
                time_minute = Convert.ToString(dr2.GetValue(0));
            }
            if (time_minute.Length == 1) time_minute = String.Concat("0", time_minute);
            Global_variables.current_time = String.Concat(time_hour, ":", time_minute);
            Console.WriteLine(Global_variables.current_time);
            label1.Text = "Текущее время: ";

            label1.Text = String.Concat(label1.Text, Global_variables.current_time);
        }

        

        private void Time_manage_Load(object sender, EventArgs e)
        {
            this.CurrenTime();
            
            //Console.WriteLine(Global_variables.current_date);
            label2.Text = "Текущая дата: ";
            label2.Text = String.Concat(label2.Text, Global_variables.current_date);
           
            


        }

        private void button3_Click(object sender, EventArgs e)
        {
            conn.Close();
            Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
           
        }

       
    }
}
